package io.demo.hallelujahdictionary;

import com.ibm.mq.jms.MQQueueConnectionFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jms.DefaultJmsListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.support.converter.MarshallingMessageConverter;
import org.springframework.jms.support.converter.MessageType;

import javax.jms.JMSException;

/**
 * @author lijinting01
 */
@Slf4j
@SpringBootApplication
public class HallelujahDictionaryApplication {

	public static void main(String[] args) {
		SpringApplication.run(HallelujahDictionaryApplication.class, args);
	}


	/**
	 * @return
	 */
	@Bean
	public CachingConnectionFactory cachingConnectionFactory(
			UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter) throws JMSException {
		CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
		cachingConnectionFactory.setTargetConnectionFactory(userCredentialsConnectionFactoryAdapter);
		cachingConnectionFactory.setSessionCacheSize(500);
		cachingConnectionFactory.setReconnectOnException(true);
		return cachingConnectionFactory;
	}

	/**
	 * @return
	 */
	@Bean
	public MarshallingMessageConverter marshallingMessageConverter() {
		MarshallingMessageConverter converter = new MarshallingMessageConverter();
		converter.setTargetType(MessageType.TEXT);
		return converter;
	}

	/**
	 * @param connectionFactory
	 * @param configurer
	 * @return
	 */
	@Bean
	public JmsListenerContainerFactory<?> jmsListenerContainerFactory(
			CachingConnectionFactory connectionFactory,
			DefaultJmsListenerContainerFactoryConfigurer configurer) {
		DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
		configurer.configure(factory, connectionFactory);
		return factory;
	}


	/**
	 * @param cachingConnectionFactory
	 * @return
	 */
	@Bean
	public JmsTemplate jmsOperations(CachingConnectionFactory cachingConnectionFactory) {
		JmsTemplate jmsTemplate = new JmsTemplate(cachingConnectionFactory);
		jmsTemplate.setReceiveTimeout(5000);
		return jmsTemplate;
	}

	@Bean
	public MQQueueConnectionFactory mqQueueConnectionFactory() throws JMSException {
		MQQueueConnectionFactory mqQueueConnectionFactory = new MQQueueConnectionFactory();
		mqQueueConnectionFactory.setHostName("10.10.1.249");
		mqQueueConnectionFactory.setPort(10001);
		mqQueueConnectionFactory.setQueueManager("QMG_CDC");
		mqQueueConnectionFactory.setTransportType(1);
		mqQueueConnectionFactory.setChannel("RECVQCHANNEL1");
		return mqQueueConnectionFactory;
	}

	@Bean
	public UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter(
			MQQueueConnectionFactory mqQueueConnectionFactory
	) {
		UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter = new UserCredentialsConnectionFactoryAdapter();
		userCredentialsConnectionFactoryAdapter.setUsername("SJYX");
		userCredentialsConnectionFactoryAdapter.setPassword("SJYX");
		userCredentialsConnectionFactoryAdapter.setTargetConnectionFactory(mqQueueConnectionFactory);
		return userCredentialsConnectionFactoryAdapter;
	}

}
